﻿using System;
using System.Windows;
using System.Threading;
using System.Collections.ObjectModel;

// Toolkit namespace
using SimpleMvvmToolkit;
using System.Diagnostics;

namespace ToolRental.Client.SMvvm.Customer.ViewModels
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvmprop</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// </summary>
    public class OrderViewModel : ViewModelBase<OrderViewModel>, INotifyError
    {
        private IOrderListServiceAgent serviceAgent;

        #region Constructor
        public OrderViewModel() {}

        public OrderViewModel(IOrderListServiceAgent serviceAgent)
        {
            this.serviceAgent = serviceAgent;
            LoadData();
        }
        #endregion

        #region LoadData
        public void LoadData()
        {
            FillOrderList();
        }

        private async System.Threading.Tasks.Task FillOrderList()
        {
            var orders = await serviceAgent.GetDeviceList(userId,this);
            OrderList = new ObservableCollection<Order>(orders);
        }
        #endregion

        #region DoOrder
        public void DoOrder()
        {
            if (selectedIndex >= 0)
            {
                if ((selectedOrder.Borrowed == false) && (selectedOrder.Ordered == false))
                {
                    serviceAgent.DoOrder(SelectedOrder.BorrowId, userId, this);
                }
                else
                {
                    ErrorOccured("Erre a készülékre már adott le rendelést!", null);
                }
            }
            else
            {
                ErrorOccured("Kérem jelölje ki a megrendelendő terméket",null);
            }
        }
        #endregion

        public event EventHandler<NotificationEventArgs<Exception>> ErrorNotice;

        #region MVVM properties

        private ObservableCollection<Order> orderList;
        public ObservableCollection<Order> OrderList
        {
            get { return orderList; }
            set
            {
                orderList = value;
                NotifyPropertyChanged(vm => vm.OrderList);
            }
        }

        private int selectedIndex;
        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                selectedIndex = value;
                NotifyPropertyChanged(s => s.SelectedIndex);
            }
        }

        private Order selectedOrder;
        public Order SelectedOrder
        {
            get { return selectedOrder; }
            set
            {
                selectedOrder = value;
                NotifyPropertyChanged(s => s.SelectedOrder);
            }
        }

        private int userId = 1;

        #endregion

        #region NotifyError

        // Helper method to notify View of an error
        private void NotifyError(string message, Exception error)
        {
            // Notify view of an error
            Notify(ErrorNotice, new NotificationEventArgs<Exception>(message, error));
        }

        #endregion

        public void ErrorOccured(string message, Exception e)
        {
            MessageBox.Show(message);
        }
    }
}